package com.sphd.emanu.fragment.details.hdetailitem;


import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.sphd.emanu.R;
import com.sphd.emanu.config.Constant;
import com.sphd.emanu.mvp.MVPBaseFragment;
import com.sphd.emanu.pojo.bank.AccountDetailsBean;
import com.sphd.emanu.pojo.bank.DetailsInfo;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * MVPPlugin
 * 邮箱 784787081@qq.com
 */

public class HDetailItemFragment extends MVPBaseFragment<HDetailItemContract.View, HDetailItemPresenter> implements HDetailItemContract.View {

    @BindView(R.id.out_in_money)
    TextView outInMoney;
    @BindView(R.id.shou_text)
    TextView shouText;
    @BindView(R.id.income_bank_name)
    TextView incomeBankName;
    @BindView(R.id.income_bank_num)
    TextView incomeBankNum;
    @BindView(R.id.out_in_image)
    ImageView outInImage;
    @BindView(R.id.fu_text)
    TextView fuText;
    @BindView(R.id.out_bank_name)
    TextView outBankName;
    @BindView(R.id.out_bank_num)
    TextView outBankNum;
    @BindView(R.id.ju_text)
    TextView juText;
    @BindView(R.id.project_create_time)
    TextView projectCreateTime;
    @BindView(R.id.project_name)
    TextView projectName;
    @BindView(R.id.project_type)
    TextView projectType;
    @BindView(R.id.project_rote)
    TextView projectRote;
    Unbinder unbinder;
    private AccountDetailsBean accountDetailsBean;
    Bundle bundle;
    @Override
    protected int getLayoutId() {
        return R.layout.fragment_detail_h;
    }

    @Override
    protected void initView() {
        unbinder = ButterKnife.bind(this, rootView);
        bundle = getArguments();
        accountDetailsBean = (AccountDetailsBean) bundle.getSerializable(Constant.ACCOUNT_DETAILS_BEAN);
        setUserVisibleHint(true);
    }

    @Override
    protected void initData() {
        showProgressDialog();
        mPresenter.getDetail(accountDetailsBean.getId());
    }

    @Override
    public void setBean(DetailsInfo detailsInfo) {
        outInMoney.setText("是否调用");
        if (accountDetailsBean.getDebit() > 0) {//存现金支出大于0就是现金到银行卡否则就是在银行卡信息中显示的是收入
            outInMoney.setText("-" + accountDetailsBean.getDebit());
            outBankName.setText(splitNotNumber(accountDetailsBean.getAccountBank()));
            outBankNum.setText(getNumbers(accountDetailsBean.getAccountBank()));
            incomeBankName.setText(splitNotNumber(accountDetailsBean.getOppositeAccount()));
            incomeBankNum.setText(getNumbers(accountDetailsBean.getOppositeAccount()));
        } else {
            outInMoney.setText("+" + accountDetailsBean.getCredit());
            incomeBankName.setText(splitNotNumber(accountDetailsBean.getAccountBank()));
            incomeBankNum.setText(getNumbers(accountDetailsBean.getAccountBank()));
            outBankName.setText(splitNotNumber(accountDetailsBean.getOppositeAccount()));
            outBankNum.setText(getNumbers(accountDetailsBean.getOppositeAccount()));
        }
        outInImage.setImageResource(R.mipmap.up_arrow);
        projectCreateTime.setText(accountDetailsBean.getCreateDate());
        projectName.setText("内部非支出性转账的除去取现金的数据");
        projectType.setText(accountDetailsBean.getType().equals("2") ? "转账交易" : "不知道");
        projectRote.setText(accountDetailsBean.getMemo());
    }
    public String getNumbers(String content) {
        Pattern pattern = Pattern.compile("\\d+");
        Matcher matcher = pattern.matcher(content);
        while (matcher.find()) {
            return matcher.group(0);
        }
        return "";
    }
    // 截取非数字
    public String splitNotNumber(String content) {
        Pattern pattern = Pattern.compile("\\D+");
        Matcher matcher = pattern.matcher(content);
        while (matcher.find()) {
            return matcher.group(0);
        }
        return "";
    }

    public String getType(AccountDetailsBean accountDetailsBean) {

//        type（项目 1-初始金额冲减,2-转帐交易,3-收入,4-支出
        switch (accountDetailsBean.getType()) {
            case "1":
                return "初始金额冲减";
            case "2":
                return "转帐交易";
            case "3":
                return "收入";
            case "4":
                return "支出";
            default:
                return "其他";

        }
    }
    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }
}
